﻿Public Class jobOffer

    Dim sumOfPercent = 100
    Dim subOfPercent As Integer
    Dim leftOver As Integer
    Dim sumPrevious As Integer
    Dim flag As Boolean
    Dim trckBrList As New List(Of TrackBar)
    Dim nmrUpDwnList As New List(Of NumericUpDown)
    Dim bSalaryValidated As Boolean

    Private Sub trckBrDegree_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trckBrDegree.Scroll
        nmrUpDwnDegree.Value = Convert.ToInt32(trckBrDegree.Value)
        If Me.trckBrList.Count > 0 Then
            percentBalance(sender)
        End If
    End Sub

    Private Sub trckBrMaster_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trckBrMaster.Scroll
        nmrUpDwnMaster.Value = Convert.ToInt32(trckBrMaster.Value)
        If Me.trckBrList.Count > 0 Then
            percentBalance(sender)
        End If
    End Sub

    Private Sub trckBrPhd_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trckBrPhd.Scroll
        nmrUpDwnPhd.Value = Convert.ToInt32(trckBrPhd.Value)
        If Me.trckBrList.Count > 0 Then
            percentBalance(sender)
        End If
    End Sub

    Private Sub trckBrArmy_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trckBrArmy.Scroll
        nmrUpDwnArmy.Value = Convert.ToInt32(trckBrArmy.Value)
        If Me.trckBrList.Count > 0 Then
            percentBalance(sender)
        End If
    End Sub

    Private Sub trckBrForeignLanguage_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trckBrForeignLanguage.Scroll
        nmrUpDwnForeignLanguage.Value = Convert.ToInt32(trckBrForeignLanguage.Value)
        If Me.trckBrList.Count > 0 Then
            percentBalance(sender)
        End If
    End Sub

    Private Sub trckBrJobType_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trckBrJobType.Scroll
        nmrUpDwnJobType.Value = Convert.ToInt32(trckBrJobType.Value)
        If Me.trckBrList.Count > 0 Then
            percentBalance(sender)
        End If
    End Sub

    Private Sub nmrUpDwnDegree_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nmrUpDwnDegree.ValueChanged
        trckBrDegree.Value = Convert.ToInt32(nmrUpDwnDegree.Value)
        trckBrDegree_Scroll(Me.trckBrDegree, e)
    End Sub

    Private Sub nmrUpDwnMaster_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nmrUpDwnMaster.ValueChanged
        trckBrMaster.Value = Convert.ToInt32(nmrUpDwnMaster.Value)
        trckBrMaster_Scroll(Me.trckBrMaster, e)
    End Sub

    Private Sub nmrUpDwnPhd_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nmrUpDwnPhd.ValueChanged
        trckBrPhd.Value = Convert.ToInt32(nmrUpDwnPhd.Value)
        trckBrPhd_Scroll(Me.trckBrPhd, e)
    End Sub

    Private Sub nmrUpDwnForeignLanguage_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nmrUpDwnForeignLanguage.ValueChanged
        trckBrForeignLanguage.Value = Convert.ToInt32(nmrUpDwnForeignLanguage.Value)
        trckBrForeignLanguage_Scroll(Me.trckBrForeignLanguage, e)
    End Sub

    Private Sub nmrUpDwnJobType_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nmrUpDwnJobType.ValueChanged
        trckBrJobType.Value = Convert.ToInt32(nmrUpDwnJobType.Value)
        trckBrJobType_Scroll(Me.trckBrJobType, e)
    End Sub

    Private Sub nmrUpDwnArmy_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nmrUpDwnArmy.ValueChanged
        trckBrArmy.Value = Convert.ToInt32(nmrUpDwnArmy.Value)
        trckBrArmy_Scroll(Me.trckBrArmy, e)
    End Sub

    Private Sub txtSalary_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSalary.KeyPress
        If Not IsNumeric(e.KeyChar) AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub jobOffer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DegreeTableAdapter.Fill(Me.DatabaseDataSet.Degree)
        Me.JobTypeTableAdapter.Fill(Me.DatabaseDataSet.JobType)
        Me.CountryTableAdapter.Fill(Me.DatabaseDataSet.Country)
        Me.CityTableAdapter.Fill(Me.DatabaseDataSet.City)
        Me.cboForeignLanguage.SelectedIndex = 0

        '===============================================================
        ' Lists of Track Bar and Numeric Up Down
        '---------------------------------------------------------------
        'Purpose : Lists are being used in percentBalance() Function
        '
        ' Author : Kwtteras Dimitrios, 12 December 2012
        '===============================================================
        trckBrList.Add(Me.trckBrDegree)
        trckBrList.Add(Me.trckBrMaster)
        trckBrList.Add(Me.trckBrPhd)
        trckBrList.Add(Me.trckBrArmy)
        trckBrList.Add(Me.trckBrForeignLanguage)
        trckBrList.Add(Me.trckBrJobType)

        nmrUpDwnList.Add(Me.nmrUpDwnDegree)
        nmrUpDwnList.Add(Me.nmrUpDwnMaster)
        nmrUpDwnList.Add(Me.nmrUpDwnPhd)
        nmrUpDwnList.Add(Me.nmrUpDwnArmy)
        nmrUpDwnList.Add(Me.nmrUpDwnForeignLanguage)
        nmrUpDwnList.Add(Me.nmrUpDwnJobType)

    End Sub

    '===============================================================
    ' Track Bars Automatic Scroll
    '---------------------------------------------------------------
    'Purpose : Track Bars are scrolling automatically while you make
    '          changes in one of them to keep 100% of percentage
    '
    ' Author : Kwtteras Dimitrios, 12 December 2012
    '===============================================================
    Private Sub percentBalance(ByVal sender As Object)
        Me.subOfPercent = 0
        Me.leftOver = 0
        Me.sumPrevious = 0

        For i As Integer = 0 To Me.trckBrList.IndexOf(sender)
            Me.sumPrevious += Me.trckBrList(i).Value
        Next

        subOfPercent = sumOfPercent - sumPrevious

        If subOfPercent < 0 Then
            sender.Value += subOfPercent
            For i As Integer = (Me.trckBrList.IndexOf(sender) + 1) To (Me.trckBrList.Count - 1)
                Me.trckBrList(i).Value = 0
            Next
        Else
            If Not sender.Equals(Me.trckBrList(Me.trckBrList.Count - 1)) Then
                leftOver = Math.Floor(subOfPercent / ((Me.trckBrList.Count - 1) - Me.trckBrList.IndexOf(sender)))
                For i As Integer = (Me.trckBrList.IndexOf(sender) + 1) To (Me.trckBrList.Count - 2)
                    Me.trckBrList(i).Value = leftOver
                Next
                Me.trckBrList(Me.trckBrList.Count - 1).Value = subOfPercent - (((Me.trckBrList.Count - 2) - Me.trckBrList.IndexOf(sender)) * leftOver)
            Else
                Me.trckBrList(Me.trckBrList.Count - 2).Value = sumOfPercent - (sumPrevious - Me.trckBrList(Me.trckBrList.Count - 2).Value)
            End If
        End If

        nmrUpDwnUpdate()
    End Sub

    '===============================================================
    ' Numeric Up Down List Update
    '---------------------------------------------------------------
    'Purpose : Numeric Up Down List values are updated
    '
    ' Author : Kwtteras Dimitrios, 12 December 2012
    '===============================================================
    Private Sub nmrUpDwnUpdate()
        For i As Integer = 0 To (Me.nmrUpDwnList.Count - 1)
            Me.nmrUpDwnList(i).Value = trckBrList(i).Value
        Next
    End Sub

    '===============================================================
    ' Validating JobOffer's Fields
    '---------------------------------------------------------------
    'Purpose : Checks if job offer's fields are valid and returns true 
    '          if they are, false if they aren't.
    '
    ' Author : Chatzigeorgiou Konstantinos, 5 January 2012
    '===============================================================
    Public Function ValidateFields() As Boolean
        Return bSalaryValidated
    End Function

    Private Sub txtSalary_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtSalary.Validating
        If txtSalary.TextLength = 0 Then
            bSalaryValidated = False
            SalaryErrorProvider.SetError(txtSalary, "Salary can't be empty")
        ElseIf Convert.ToDouble(txtSalary.Text) < 300 Then
            bSalaryValidated = False
            SalaryErrorProvider.SetError(txtSalary, "Salary can't be less than 300€")
        Else
            bSalaryValidated = True
            SalaryErrorProvider.Clear()
        End If
    End Sub

    Private Sub CountryBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CountryBindingSource.CurrentChanged

    End Sub

    Private Sub CountryCityBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CountryCityBindingSource.CurrentChanged

    End Sub

    Private Sub JobTypeBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JobTypeBindingSource.CurrentChanged

    End Sub

    Private Sub ToolTip1_Popup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PopupEventArgs) Handles ToolTip1.Popup

    End Sub

    Private Sub DegreeBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DegreeBindingSource.CurrentChanged

    End Sub
End Class
